package com.zysc.processflow.common.util;

import com.alibaba.fastjson2.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class SendSMSUtils {
    @Value("${sendMsg.enable:true}")
    private boolean sendMsg;
    private static Logger logger = LoggerFactory.getLogger(SendSMSUtils.class);

        public static void main(String[] args) {
        try{
            String ak="XbiV9jdE8nSrgX2zEJOVWtvQ8i6UhZMA";
            String sk="vjcNk6i1SOD3WtRbdpDbeR6bIxg18n3C";
            long time = System.currentTimeMillis();
            String sign=getMD5By32(time + sk);
            //String url="http://10.27.165.97:8800/xasupport/smsapi/sendsms/?";
            String url="https://xasz.0570w.net/xasupport/smsapi/sendsms/?"; //公网地址
            String param="phone=18854805007&str="+ URLEncoder.encode("浙江衢州化工材料有限公司-刘乙江，您好！化工企业测试账号,2023-12-06 15:34:27发生安全生产费用提取使用不足报警，报警内容为：(统计时间2023年12月06日):1、你单位2023年度1-14月安全生产费用应提取3986045.58元，已提取3623677.72元，未足额提取；2、你单位2023年度1-11月安全生产费用已支出0元，占1-11月应计提金额的0%，占比过低。请发生报警的企业及时处置，并在1小时内反馈处置情况!详情见[http://220.191.236.172:6040/u/2fefab003583d046]","UTF-8");
            param+="&ak="+ak+"&sign="+sign+"&time="+time;
            System.out.println(url+param);
            String res=HttpUtils.get(url+param);
            System.out.println(res);
            Map map = JSON.parseObject(res, Map.class);
            int code = (Integer)map.getOrDefault("code", 500);
            System.out.println(code);
        }catch(Exception e){
            logger.error("转换失败");
        }

    }
    public static Map sendMsg(String phone,String message){
        Map map = new HashMap<>();
        try{
            //1.配置参数
            String url="https://xasz.0570w.net/xasupport/smsapi/sendsms/?";
            String ak="XbiV9jdE8nSrgX2zEJOVWtvQ8i6UhZMA";
            String sk="vjcNk6i1SOD3WtRbdpDbeR6bIxg18n3C";
            long time = System.currentTimeMillis();
            String sign=getMD5By32(time + sk);
            String param="phone="+phone+"&str="+ URLEncoder.encode(message,"UTF-8");
            param+="&ak="+ak+"&sign="+sign+"&time="+time;
            String result = "";
            //2.发送短信
            result=HttpUtils.get(url+param);
            //3.返回结果
            map = JSON.parseObject(result, Map.class);
            int code = (Integer)map.getOrDefault("code", 500);
            if(code!=200){
                throw new RuntimeException(map.getOrDefault("msg","发送失败，请检查！").toString());
            }else{
                logger.info("发送给"+phone+"信息成功！信息内容："+message);
            }
        }catch(Exception e){
            logger.error("发送给"+phone+"信息失败,信息内容:"+message+"  系统反馈的问题："+ e.getMessage());
        }
        return map;
    }

    public static String getMD5By32(String plainText) {
        String re_md5 = new String();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            re_md5 = buf.toString();
            return re_md5;
        } catch(Exception e){
            System.out.println("计算MD5值发生错误");
            e.printStackTrace();
        }
        return null;
    }

    //将程序遇到的报错发给内部人员 ,开发时可以在程序catch中调用此方法,便于及时发现问题
    public static void sendErrorMsgToInsider(String message){
        String phone = "15621797601";//刘乙江15621797601 , 手机号码以逗号可开
        sendMsg(phone,message);
    }
}
